﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wolfbyte.Core.Data
{
    public class OnePerLocalContextFactory<T> : IFactory<T>
    {
        private IFactory<T> InnerFactory;

        public OnePerLocalContextFactory(IFactory<T> InnerFactory)
        {
            this.InnerFactory = InnerFactory;
        }

        #region IFactory<T> Members

        public T Create()
        {
            var key = InnerFactory.GetType().Name;

            if (Local.Data.ContainsKey(key) == false)
                Local.Data[key] = InnerFactory.Create();
            return (T)Local.Data[key];
        }

        #endregion
    }
}